package com.yundingai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yundingai.common.RabbitSender;
import com.yundingai.domain.entity.RoomChat;
import com.yundingai.domain.vo.RoomChatVo;
import com.yundingai.mapper.AllUserMapper;
import com.yundingai.service.RoomChatService;
import com.yundingai.mapper.RoomChatMapper;
import com.yundingai.utils.BeanCopyUtils;
import com.yundingai.utils.RedisUtils;
import com.yundingai.vm.constant.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author liyuxiang
* @description 针对表【room_chat(聊天记录表：记录每个自习室中的聊天记录)】的数据库操作Service实现
* @createDate 2025-07-16 12:43:19
*/
@Service
public class RoomChatServiceImpl extends ServiceImpl<RoomChatMapper, RoomChat>
    implements RoomChatService{
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    RabbitSender rabbitSender;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    RoomChatMapper roomChatMapper;
    @Autowired
    AllUserMapper allUserMapper;

    @Override
    public void handleChatMessage(RoomChatVo roomChatVo) {
        // 处理聊天消息的逻辑
        // 这里可以解析消息内容，存储到数据库，或者进行其他业务处理
        System.out.println("处理聊天消息: " + roomChatVo.toString());
//        // 示例：将消���存储到数据库
//        RoomChat roomChat = new RoomChat();
//        roomChat.setMessage(roomChat);
        redisUtils.lpush(RedisConstant.CHAT_ROOM_PREFIX + roomChatVo.getRoomId(), roomChatVo.toString());
        RoomChat roomChat = BeanCopyUtils.copyBean(roomChatVo, RoomChat.class);
        this.save(roomChat);
    }

    @Override
    public void send(RoomChatVo roomChatVo) {
        rabbitSender.sendChat(roomChatVo);
    }

    @Override
    public List<RoomChatVo> getRoomChatList(String roomId) {

        List<RoomChatVo> roomChatVoList = roomChatMapper.selectByRoomId(roomId);
        return roomChatVoList;
    }
}




